package com.fg.cloud.manager.service.agency.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.construction.ResultMap;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.AgencyAddressEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyAddressVo;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyAddressMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.agency.AgencyAddressService;

/**
 * 经销商信息service实现
 * @author around
 * @date 2017-6-21
 */
@Service
public class AgencyAddressServiceImpl extends BaseServiceImpl implements AgencyAddressService {

	@Autowired
	private AgencyAddressMapper agencyAddressMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return agencyAddressMapper;
	}

	@Override
	public void findList(PageUtils<AgencyAddressEntity> pageinfo) {
		long total = agencyAddressMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if(total > 0) {
			pageinfo.setData(agencyAddressMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<AgencyAddressEntity>());
		}
	}

	@Override
	@Transactional
	public boolean deletes(Long userId, Long ageId, Long[] ids) {
		return agencyAddressMapper.updateStatus(userId, ageId, ids, -1) > 0 ? true : false;
	}
	
	@Override
	public boolean remove(Long ageId, Long[] ids) {
		return agencyAddressMapper.remove(ageId, ids) > 0 ? true : false;
	}

	@Override
	public AgencyAddressEntity find(Long id) {
		return agencyAddressMapper.selectByPrimaryKey(id);
	}

	@Override
	public AgencyAddressEntity selectBycondition(AgencyAddressEntity searchVo) {
		return agencyAddressMapper.selectBycondition(searchVo);
	}

	@Override
	public void selectforPage(PageUtils<AgencyAddressEntity> pageinfo) {
		long total = agencyAddressMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(agencyAddressMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<AgencyAddressEntity>());
		}
	}

	@Override
	public List<AgencyAddressEntity> selectAll(Long ageId) {
		return agencyAddressMapper.selectAll(ageId);
	}

	@Override
	@Transactional
	public boolean cancels(Long userId, Long ageId, Long[] ids) {
		
		return agencyAddressMapper.updateIsdefault(userId, ageId, ids, 0) > 0 ? true : false;
	}

	@Override
	@Transactional
	public boolean setdefault(Long userId, Long ageId, Long id) {
		
		return agencyAddressMapper.setdefault(userId, ageId, id, 1) > 0 ? true : false;
	}
	
	@Override
	@Transactional
	public boolean updateByPrimaryKeySelective(AgencyAddressVo address) {
		if (1 == address.getIsDefault()) {
			agencyAddressMapper.resetDefault(address.getAgeId());
		} else {
			if (null == selectDefault(address.getAgeId()))
				address.setIsDefault(1);
		}
		return agencyAddressMapper.updateByPrimaryKeySelective(address) > 0 ? true : false;
	}
	
	
	@Override
	@Transactional
	public boolean insertSelective(AgencyAddressVo address) {
		if (1 == address.getIsDefault()) {
			agencyAddressMapper.resetDefault(address.getAgeId());
		} else {
			if (null == selectDefault(address.getAgeId()))
				address.setIsDefault(1);
		}
		return agencyAddressMapper.insertSelective(address) > 0 ? true : false;
	}

	@Override
	public boolean verifyUniquely(AgencyAddressVo address) {
		return agencyAddressMapper.verifyUniquely(address) > 0 ? false : true;
	}

	@Override
	public AgencyAddressEntity findOne(Long id, Long ageId){
		return agencyAddressMapper.findOne(id, ageId);
	}
	
	@Override
	public boolean judgeIsExit(AgencyAddressVo address) {
		
		return agencyAddressMapper.judgeIsExit(address) > 0 ? true : false ;
	}

	@Override
	public boolean JudgeIsExit(AgencyAddressVo address) {
		
		return agencyAddressMapper.JudgeIsExit(address) > 0 ? true : false ;
	}

	/**
	 * 新增保存
	 */
	@Override
	@Transactional
	public boolean insertAdd(AgencyAddressVo address, AgencyUserVo user) {
		//处理省市区
		if(StringUtils.isNotEmpty(address.getAdministrative())) {
			if(address.getAdministrative().length() > 0) {
				List<ResultMap> array = JSON.parseArray(address.getAdministrative(), 
						ResultMap.class);
				if (array.size() == 3)
					address.setCity(array.get(1).getLabel());
				else
					address.setCity(array.get(0).getLabel());
				address.setArea(array.get(array.size()-1).getLabel());
				address.setProvince(array.get(0).getLabel());
			}
		}
		//处理邮编
		if ( null == address.getZipcode() ) address.setZipcode("000000");
		//处理默认收货地址
		if ( null == address.getIsDefault() ) address.setIsDefault(0);
		if ( 1 == address.getIsDefault() ) {
			agencyAddressMapper.resetDefault( address.getAgeId() );
		}
		address.setCreator( user.getId() );
		address.setUpdater( user.getId() );
		int flag = agencyAddressMapper.insertSelective(address);
		return flag > 0 ? true : false;
	}

	/**
	 * 编辑保存
	 */
	@Override
	@Transactional
	public boolean saveAdd(AgencyAddressVo address, AgencyUserVo user, AgencyAddressEntity findOne) {
		//处理省市区
		if(StringUtils.isNotEmpty(address.getAdministrative())) {
			if(address.getAdministrative().length() > 0) {
				List<ResultMap> array = JSON.parseArray(address.getAdministrative(), 
						ResultMap.class);
				if (array.size() == 3)
					address.setCity(array.get(1).getLabel());
				else
					address.setCity(array.get(0).getLabel());
				address.setArea(array.get(array.size()-1).getLabel());
				address.setProvince(array.get(0).getLabel());
			}
		}
		//处理默认地址
		if ( null == address.getIsDefault() ) address.setIsDefault(findOne.getIsDefault());	
		address.setUpdater( user.getId() );
		
		boolean flag = updateByPrimaryKeySelective(address);
		return flag;
	}

	@Override
	public boolean finds(Long id, Long ageId) {
		
		return agencyAddressMapper.finds(id, ageId) > 0 ? true : false;
	}

	/**
	 * 查询默认收货地址
	 */
	@Override
	public AgencyAddressEntity selectDefault(Long ageId) {
		
		return agencyAddressMapper.selectDefault(ageId);
	}


}
